<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once 'model/db.php';
?>

<h1 >Tran Thi Thu</h1>
<?php
$query = "SELECT * FROM companies";

$companies = getPage($query);
$query = "SELECT * FROM apps";
$apps = getPage($query);
?>
<form method="POST" >
    Keyword <input type="text" name="keyword" placeholder="Keyword"><br>
    Min fans <input type="text" name="min" placeholder="Min fans"><br>
    Max fans <input type="text" name="max" placeholder="Max fans"><br>
    Company  <select name="id_company">
        
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
        </pre>
    </select><br>
    App  <select name="id_app">
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
        <?php endforeach; ?>
    </select ><br>

    <input type="submit" name="submit" value="gui">
</form>

<?php if (isset($_POST['submit'])): ?>

    <?php
    $min_fans = $_POST['min'];
    $max_fans = $_POST['max'];
    $keyword = $_POST['keyword'];
    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  WHERE
                      likes >= $min_fans
                      AND description LIKE '% $keyword %'                  
                  LIMIT 0, 10
                  ";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                      
                  LIMIT 0, 10
                  ";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
             WHERE (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else {
        $query = "SELECT * FROM pages    
                  WHERE
                      (likes >= $min_fans)
                      AND (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    }
    var_dump($query);
    $pages = getPage($query);
    ?>
<?php endif; ?>
<?php if (!empty($pages)): ?>
    <table border='1'>
        <tr >
            <td> profile </td>
            <td> likes </td>
            <td> description</td>

        </tr>
        <?php foreach ($pages as $page): ?>
            <tr>
                <td><img src="<?php echo $page['picture'] ?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td><?php echo substr($page['description'], 0, 100) ?></td>

                <?php ?>
            </tr>
        <?php endforeach; ?>
    </table>  

<?php endif; ?>

